package cn.wolfcode.business.controller;

import cn.wolfcode.business.domain.BusAudit;
import cn.wolfcode.business.service.BusAuditService;
import cn.wolfcode.common.core.controller.BaseController;
import cn.wolfcode.common.core.domain.AjaxResult;
import cn.wolfcode.common.core.page.TableDataInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static cn.wolfcode.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/business/audit")
public class BusAuditController extends BaseController {
    @Autowired
    BusAuditService busAuditService;

    /**
     * 查询所有的审核
     *
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo getAuditList() {
        startPage();
        List<BusAudit> auditList = busAuditService.getAuditList();
        return getDataTable(auditList);
    }

    /**
     * 查询当前流程实例的进度
     * @param instanceId  流程实例id
     * @param response  响应对象
     * @throws IOException  抛出异常
     */
    @GetMapping("/progress/{instanceId}")
    public void getAuditprogressPng(@PathVariable Long instanceId, HttpServletResponse response) throws IOException {
        InputStream auditprogressPng = busAuditService.getAuditprogressPng(instanceId);
        IOUtils.copy(auditprogressPng, response.getOutputStream());
    }

    /**
     * 撤销当前流程实例
     * @param id
     * @return
     */
    @DeleteMapping("/cancel/{id}")
    public AjaxResult deleteCancelAudit(@PathVariable Long id){
        return  AjaxResult.success(busAuditService.deleteCancelAudit(id));
    }
}
